package com.eeepay.eeepay_v2.app;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.multidex.MultiDex;

import com.eeepay.eeepay_v2.BuildConfig;
import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.excation.CrashHandlerManager;
import com.eeepay.eeepay_v2.sqlite.DBHelper;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.util.SoundPlayUtils;
import com.eeepay.v2_library.util.ImageLoaderUtils;
import com.tencent.tinker.loader.app.ApplicationLike;
import com.tinkerpatch.sdk.TinkerPatch;
import com.tinkerpatch.sdk.loader.TinkerPatchApplicationLike;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.common.QueuedWork;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;

import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.ONLINE_PORT;
import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.ONLINE_URL;
import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.TEST_KEY;
import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.TEST_PORT;
import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.TEST_URL;
import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.ZSC_PORT;
import static com.eeepay.eeepay_v2.activity.TestUpdateIPAct.ZSC_URL;

/**
 * Created by zw on 2016/3/1 0001.
 */
public class MyApplication extends Application {

    public static Context context;
    private static MyApplication instance;

    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static Map<String, ?> maps = null;
    public static DBHelper dbHelper;

    public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD1mecWBLMB1snW" +
            "3J089PGK/yICyWRzXnheUuIHD756S9g9XT0QqeR2l8k8L946VnTWLm3QmtpkS32c2ejfarvVnzkuJ" +
            "rYZyGZivN2hswz+PRxwresR8n/8NQOJ9hu9XVURL24owRKICQg5pD3lqRVL0MFxW+BJB/BZn+uSUFQMIw" +
            "IDAQAB";

    public boolean isLogin = false;//登陆标志
    public boolean hasNews = false;//新消息标志
    private ApplicationLike tinkerApplicationLike;
    public static MyApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();


        instance = this;
        /** 初始化imageloader ***/
        ImageLoaderUtils.initImageLoader(getApplicationContext()); // 初始化图片加载器
//		ABPreferenceUtils.getInstance(getApplicationContext());

//        LeakCanary.install(this);
//        MultiDex.install(this) ;

        context = getApplicationContext();

        if (dbHelper == null) {
            dbHelper = new DBHelper(this);
        }
        //add by zhuangzeqin 2017年4月27日14:50:01 初始化异常处理
        initCrashHandler();
        //add by zhuangzeqin 2017年7月18日15:34:55 初始化友盟分享sdk
        initUMShareConfig();
        initTinker();

        //每次启动去获取当前的环境状况；是测试环境还正式环境
        changeUrl();

        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush

        /** add by zhuangzeqin 2017年9月12日10:17:39 初始化默认的声音加载 **/
        SoundPlayUtils.getInstance().initDefaultSoundPool(context);
    }

    /**
     * 每次启动去获取当前的环境状况；是测试环境还正式环境
     */
    private void changeUrl() {
        int testkey = (int) SPUtils.get(TEST_KEY, 0);
        switch (testkey) {
            case 0://线上环境（生产环境）
                //如果
                String url = (String)SPUtils.get(ONLINE_URL, Constant.URL.URL);
                String port = (String)SPUtils.get(ONLINE_PORT,Constant.URL.PORT);
                Constant.URL.URL = url;
                Constant.URL.PORT = port;
                break;
            case 1://add by zhuangzeqin 2017年10月18日17:52:48 准生产环境
                String zsc_url = (String)SPUtils.get(ZSC_URL, Constant.URL.URL);
                String zsc_port = (String)SPUtils.get(ZSC_PORT,Constant.URL.PORT);
                Constant.URL.URL = zsc_url;
                Constant.URL.PORT = zsc_port;
                break;
            case 2://182环境
                String test_url = (String)SPUtils.get(TEST_URL, Constant.URL.URL);
                String test_port = (String)SPUtils.get(TEST_PORT,Constant.URL.PORT);
                Constant.URL.URL = test_url;
                Constant.URL.PORT = test_port;
                break;
        }
    }

    //初始化友盟分享sdk
    private void initUMShareConfig()
    {
        //开启debug模式，方便定位错误，具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integration的报错必看，正式发布，请关闭该模式
        Config.DEBUG = true;
        QueuedWork.isUseThreadPool = false;
        UMShareAPI.get(this);
        //各个平台的配置，建议放在全局Application或者程序入口
        String appId = getResources().getString(R.string.str_appid);
        String appsecret = getResources().getString(R.string.str_appsecret);
        PlatformConfig.setWeixin(appId,appsecret);
    }

    private void initTinker() {
        if (BuildConfig.TINKER_ENABLE) {
            tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike();
            // 初始化TinkerPatch SDK
            TinkerPatch.init(tinkerApplicationLike)
                    .reflectPatchLibrary()
                    .setPatchRollbackOnScreenOff(true)
                    .setPatchRestartOnSrceenOff(true)
                    .setFetchPatchIntervalByHours(3);
            // 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果
            TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
            //immediately 为 true, 每次强制访问服务器更新
//            TinkerPatch.with().fetchPatchUpdate(true);
       }
    }


    /**
     * 初始化异常处理
     */
    private void initCrashHandler() {
        new CrashHandlerManager(getApplicationContext());
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base); MultiDex.install(this);
    }
}
